﻿using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using LASDesktop.Classes;
using LASDesktop.LASDBClasses;

namespace LASDesktop.ModuloSocios 
{
    
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Proveedores : ISocios
    {
        private LASDBDataContext _db = new LASDBDataContext();
        private readonly int _companyId;
        private Login _parent;

        public Proveedores(Login parent)
        {
            //TODO: Obtener compañia
            _companyId = 1;
            _parent = parent;
            InitializeComponent();
            LoadPartners();
            LoadPartnerCodes();
        }

        #region Socios

        public void SetOverlayVisibilityHidden()
        {
            Overlay.Visibility = Visibility.Hidden;
        }

        public void LoadPartners()
        {
            _db = new LASDBDataContext();
            var partnersList = GetPartnersFromCompany();

            if (partnersList == null)
            {
                //TODO: Desplegar error
                return;
            }

            GridSocios.ItemsSource = partnersList;
        }
        
        private void ViewDetailsButtonClick(object sender, RoutedEventArgs e)
        {
            var socio = (GetPartnersResult)GridSocios.SelectedItem;
            
            var ventanaDetallesSocio = new VisorDeSocios(this, socio, _companyId);
            Overlay.Visibility = Visibility.Visible;
            ventanaDetallesSocio.ShowDialog();
        }

        private void SubmitButtonClick(object sender, RoutedEventArgs e)
        {
            var ventanaNuevoSocio = new VisorDeSocios(this, _companyId);
            Overlay.Visibility = Visibility.Visible;
            ventanaNuevoSocio.ShowDialog();
        }

        private List<GetPartnersResult> GetPartnersFromCompany()
        {

            List<GetPartnersResult> listaDeSocios = null;

            try
            {
                listaDeSocios = _db.GetPartners(_companyId, Constants.PROVEEDOR).ToList();

            }
            catch
            {
                //TODO: Error al recuperar los articulos
            }

            return listaDeSocios;
        }



        #endregion

        #region Métodos de actualización del GUI
        /// <summary>
        /// Método que carga la lista de los códigos de los socios existentes al combobox
        /// </summary>
        private void LoadPartnerCodes()
        {
            try
            {
                var result = GetPartnersFromCompany();
                InputSocios.ItemsSource = result;
                InputSocios.DisplayMemberPath = "CodigoSocios";
                InputSocios.SelectedIndex = 0;
            }
            catch
            {
                //TODO: Error
            }
        }

        /// <summary>
        /// Método que carga la lista de los documentos de un determinado socio al grid.
        /// </summary>
        private void LoadPartnerDocuments()
        {
            try
            {
                //Actualiza el Datacontext en caso de cambios
                _db = new LASDBDataContext();

                //Obtiene los productos de la bodega actual
                var socioId = (InputSocios.SelectedItem as GetPartnersResult).IdSocio;
                var documentsList = GetPartnerDocuments(socioId);
                DocumentosGrid.ItemsSource = documentsList;
            }
            catch
            {
                
            }
        }
        #endregion

        #region Eventos del GUI

        private void SocioComboChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            var socioName = (InputSocios.SelectedItem as GetPartnersResult).Nombre;
            TextNombreSocio.Content = socioName;
            LoadPartnerDocuments();
        }
        #endregion

        #region Métodos LINQ-TO-SQL

        public List<GetPartnerDocumentsResult> GetPartnerDocuments(int idSocio)
        {
            List<GetPartnerDocumentsResult> listaDocumentos = null;

            try
            {
                listaDocumentos = _db.GetPartnerDocuments(idSocio).ToList();

            }
            catch
            {
                //TODO: Error al recuperar los articulos
            }

            return listaDocumentos;
        }
        #endregion

        #region Eventos de la ventana
        /// <summary>
        /// Método que se dispara al seleccionar una opción del menú general
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChangeWindow(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (sender.GetType() != (typeof(Label))) return;

            switch (((Label)sender).Content.ToString())
            {
                case "Inventario":
                    _parent.MostrarInventarios();
                    break;
                case "Ventas":
                    _parent.MostrarVentas();
                    break;
                case "Socios":
                    _parent.MostrarSocios();
                    break;
                case "Bancos":
                    _parent.MostrarBancos();
                    break;
                case "Compras":
                    _parent.MostrarCompras();
                    break;
            }
        }

        /// <summary>
        /// Método que se dispara al cerrar la ventana.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CloseParent(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _parent.Close();
        }

        private void VerClientes(object sender, System.Windows.RoutedEventArgs e)
        {
        	_parent.MostrarClientes();
        }
        #endregion
    }
}
